﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using DataAccessLayer.DAL;
using Common;
using System.Net.Mail;
using System.Threading;
using System.IO;

namespace BestBuy.BuyerViews
{
    public partial class SelectWonSeller : System.Web.UI.Page
    {
        private static readonly string ActiveEmailTemplate = "~/EmailTemplates/WonSeller{0}.htm";
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            Auction auction = null;
            if (Request.QueryString["AuctionID"] != null)
            {
                int id = int.Parse(Request.QueryString["AuctionID"]);
                var auc = AuctionDAL.GetAuctionByID(id);
                if (auc.Status == (int)AuctionStatus.Active || auc.Status == (int)AuctionStatus.Endtime)
                {
                    auction = auc;
                }
            }
            if (!IsPostBack)
            {
                LoadSellerList(auction);
            }
        }

        private void LoadSellerList(Auction auc)
        {
            if (auc != null)
            {
                gvSelectWonSeller.DataSource = SellerBidDAL.GetBids(auc.ID);
                gvSelectWonSeller.DataBind();
            }
        }

        private void SendEmail(string emailAdds, string fullname, string title, string price)
        {
            try
            {
                string subject = "Bạn đã chiến thắng phiên đấu giá.";
                MailMessage mailMsg = new MailMessage();
                mailMsg.Subject = subject;
                mailMsg.To.Add(emailAdds);
                mailMsg.IsBodyHtml = true;
                string htmlBody = File.ReadAllText(Server.MapPath(GetEmailTemplatePath()));
                mailMsg.Body = htmlBody.Replace("{fullname}", fullname)
                                        .Replace("{titleAuction}", title)
                                        .Replace("{price}", price);
                SmtpClient smtp = new SmtpClient();
                smtp.Send(mailMsg);
            }
            catch
            {
            }
        }

        private string GetEmailTemplatePath()
        {
            string path = ActiveEmailTemplate;

            // Current support Vietnamese
            path = string.Format(path, "");
            return path;
        }

        protected void ChooseButtion_Click(object sender, EventArgs e)
        {
            Button senderButton = sender as Button;
            int id = int.Parse(senderButton.Attributes["ObjectId"]);
            // edit here
            AuctionTransaction aucTran = new AuctionTransaction();
            SellerBid bid = SellerBidDAL.GetByID(id);
            if (bid != null)
            {
                aucTran.AuctionID = bid.AuctionID;
                aucTran.SellerID = bid.SellerID;
                aucTran.Price = bid.BidValue;
                aucTran.Status = (int)AuctionTransactionStatus.Waitting;
                aucTran.Date = DateTime.Now;
                AuctionTransactionDAL.AddAuctionTransaction(aucTran);
                //send email to seller
                User seller = UserDAL.GetUser(aucTran.SellerID);
                Auction auc = AuctionDAL.GetAuctionByID(aucTran.AuctionID);
                Thread sendmailThread = new Thread(delegate()
                {
                    SendEmail(seller.Email, seller.FirstName+" "+seller.LastName, auc.Title, aucTran.Price.ToString());
                });
                sendmailThread.IsBackground = true;
                sendmailThread.Start();

            }
        }

    }
}